<?php

namespace app\yperp\common;

use GuzzleHttp\Client as GuzzleHttpClient;

class GuzzleHttp
{
    static function get($url, $options = [])
    {
        $client = new GuzzleHttpClient([
            'verify' => env('root_path') . 'application\yperp\common\cacert.pem',
            // 忽略证书验证
        ]);

        $response = $client->request('GET', $url, $options);
        $data = $response->getBody()->getContents();
        $contentType = $response->getHeader('Content-Type')[0];
        if (!empty($contentType) && $contentType == 'application/json') {
            return json_decode($data, true);
        }
        return $data;
    }
    static function post($url, $options = [])
    {
        try {
            //code...

            $client = new GuzzleHttpClient([
                'verify' => env('root_path') . 'application\yperp\common\cacert.pem',
                // 忽略证书验证
            ]);

            $response = $client->request('POST', $url, $options);
            $data = $response->getBody()->getContents();
            $contentType = $response->getHeader('Content-Type')[0];

            if (strpos($contentType, "application/json") !== false) {
                return json_decode($data, true);
            }
            return $data;
        } catch (\Throwable $th) {
           dump($th->getMessage());
           dump($th->getTraceAsString());
        }
    }
}
